<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- 数据表格 -->
      <ele-pro-table
          ref="table"
          :datasource="url"
          :columns="columns"
          default-expand-all
          row-key="id"
          height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:abnormal:add')">添加
          </el-button>
        </template>

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <template v-if="row.id > 0">
            <el-link
                type="primary"
                :underline="false"
                icon="el-icon-edit"
                @click="openEdit(row)">修改
            </el-link>
            <el-popconfirm
                class="ele-action"
                title="确定要删除此无人机吗？"
                @confirm="remove(row)">
              <el-link
                  type="danger"
                  slot="reference"
                  :underline="false"
                  icon="el-icon-delete">删除
              </el-link>
            </el-popconfirm>
          </template>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit
        v-if="showEdit"
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import edit from './edit.vue';

export default {
  components: {edit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/uav/getList',
      // 表格列配置
      columns: [
        {
          prop: 'id',
          label: 'ID',
          showOverflowTooltip: true,
          width: 100,
          align: 'left',
        },
        {
          prop: 'no',
          label: '编号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'name',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 300
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 180,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      current: null,
      showEdit: false
    };
  },
  methods: {
    reload() {
      this.$refs.table.reload({where: this.where})
    },
    reset() {
      this.where = {}
      this.reload()
    },
    openEdit(row) {
      this.current = row
      this.showEdit = true
    },
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/uav/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style>
.el-table .module-row {
  background-color: #262626;
}
</style>
